package com.other;

import java.util.LinkedList;
import java.util.Queue;

public class XingYe {
    /**
     * 求解字符串最长子串的长度
     *
     * @param s string字符串
     * @return int整型
     */
    public int lengthOfLongestSubstring(String s) {
        // write code here
        if (s == null || s.length() == 0) {
            return 0;
        }
        if (s.length() == 1) {
            return 1;
        }
        int maxLen = 0;
        Queue<Character> queue = new LinkedList<>();
        for (char c : s.toCharArray()) {
            if (queue.size() == 0 || !queue.contains(c)) {
                queue.add(c);
            } else if (queue.contains(c)) {
                maxLen = Math.max(maxLen, queue.size());
                while (!queue.poll().equals(c)) {
                }
                queue.add(c);
            }
        }
        maxLen = Math.max(maxLen, queue.size());
        return maxLen;
    }

    public static void main(String[] args) {
        XingYe xingYe = new XingYe();
        System.out.println(xingYe.lengthOfLongestSubstring("abcdef"));
    }
}
